﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

using WeChatDemo.Models;
using WeChatDemo.Models.DTO;
using WeChatDemo.DataProvider;

namespace WeChatDemo.Controllers
{
    public class UserController : ApiController
    {
        [HttpGet]
        public IHttpActionResult Get()
        {
            IEnumerable<DTOUser> _list = null;
            _list = DAOUser.GetAllList();

            if(_list.Count<DTOUser>() == 0)
            {
                return NotFound();
            }
            else
            {
                return Ok(_list);
            }
        }

        [HttpGet]
        public IHttpActionResult Get(string id)
        {
            IEnumerable<DTOUser> _list = null;
            _list = DAOUser.GetList($"AND OpenID = '{id}'");

            if (_list.Count<DTOUser>() == 0)
            {
                return NotFound();
            }
            else
            {
                return Ok(_list.ElementAt<DTOUser>(0));
            }
        }

        [HttpPost]
        public IHttpActionResult Post([FromBody]User model)
        {
            //测试OpenID:oRMP5shI6hdqW7Fs7quu9yuU3HKg
            if (DAOUser.Add(model))
            {
                HttpResponseMessage _message = new HttpResponseMessage();
                _message.StatusCode = HttpStatusCode.Created;
                _message.Content = new StringContent("success");
                return ResponseMessage(_message);
            }
            else
            {
                return InternalServerError();
            }
        }
    }
}
